package com.coszero.utils.data

import com.coszero.utils.utils.LogX
import com.coszero.utils.utils.StringUtils
import java.io.UnsupportedEncodingException
import java.nio.charset.StandardCharsets


/**
 * Desc： ASCII码转换
 *
 * @author xmqian
 * Email:xmqian93@163.com
 * Date: 2020/9/8 0008 12:02
 * A-Z 65-90
 * a-z 97-122
 * 0-9 48-57
 */
object AsciiUtils {
    /**
     * 字符串转换为Ascii
     *
     * @param value value
     * @return
     */
    fun stringToAscii(value: String): String {
        val sbu = StringBuffer()
        val chars = value.toCharArray()
        for (i in chars.indices) {
            if (i != chars.size - 1) {
                sbu.append(chars[i].code).append(",")
            } else {
                sbu.append(chars[i].code)
            }
        }
        return sbu.toString()
    }

    /**
     * Ascii转换为字符串
     *
     * @param value
     * @return
     */
    fun asciiToString(value: String): String {
        val sbu = StringBuffer()
        val chars = value.split(",".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()
        for (i in chars.indices) {
            sbu.append(chars[i].toInt() as Char)
        }
        return sbu.toString()
    }

    /**
     * @param ascDatas ascii码的list
     * @return 返回转化拼接后的字符串
     */
    fun asciiToString(ascDatas: List<String>): String {
        if (ascDatas.size > 0) {
            val sbu = StringBuilder()
            for (i in ascDatas.indices) {
                sbu.append(ascDatas[i].toInt() as Char)
            }
            return sbu.toString()
        }
        return ""
    }

    /**
     * @param s hex
     * @return 返回16进制转化的字节数组
     * 使用方法
     * 用String包装类解析，并设置字符编码格式
     * new String(AsciiUtils.hexStringToByteArray(dataStr), "GBK");
     */
    fun hexStringToByteArray(s: String): ByteArray {
        val len = s.length
        val data = ByteArray(len / 2)
        var i = 0
        while (i < len) {
            data[i / 2] = ((s[i].digitToIntOrNull(16) ?: -1 shl 4)
            + s[i + 1].digitToIntOrNull(16)!! ?: -1).toByte()
            i += 2
        }
        return data
    }

    /**
     * @param builder 字符串
     * @return 返回转码为gb2312的16进制字符串
     */
    fun stringToGb2312Hex(builder: String): Array<String?> {
        try {
            if (!StringUtils.isEmpty(builder)) {
                val utf8 = String(builder.toByteArray(StandardCharsets.UTF_8))
                val unicode = String(utf8.toByteArray(), StandardCharsets.UTF_8)
                val gb2312s = unicode.toByteArray(charset("gb2312"))
                val gb2312HexArray = arrayOfNulls<String>(gb2312s.size)
                for (i in gb2312s.indices) {
                    val hex = Integer.toHexString(
                        DataConversion.byteToDec(
                            gb2312s[i]
                        )
                    )
                    gb2312HexArray[i] = hex
                }
                return gb2312HexArray
            }
        } catch (e: UnsupportedEncodingException) {
            e.printStackTrace()
            LogX.e("stringToGb2312Hex", "字符转码错误")
        }
        return arrayOf()
    }

    /**
     * @param str 内容
     * @return 返回16进制字符串
     */
    fun stringToGb2312HexStr(str: String): String {
        val strArray = stringToGb2312Hex(str)
        val builder = StringBuilder()
        for (i in strArray.indices) {
            builder.append(strArray[i])
        }
        return builder.toString()
    }
}

